<?php

//==================================================================
//
//  Author: ThuongLT and TuanNVM
//  email: haonhienthaibao@gmail.com
//  Name: A Simple Shell Class
//  Description: This class is written to make it easy to use Shell terminal
//  with array variables. This is exceptionally useful when dealing
//  with HTML Post variables.
//
//===================================================================


class terminal {

	var $direc="/var/shell/";
	
    public function __construct(){

         exec("sudo mkdir ".$this->direc);

    }
	
	public function get_direc($filename){
		$array=explode('/',$filename);
		for($i=0;$i<count($array);$i++)
			if($array[$i]!=null)
				if($i==(count($array)-1))
					$item.="/";
				else
					$item.="/".$array[$i];
		return $item;
    }
	
	public function get_name_file($filename){
		$array=explode('/',$filename);
		$item=$array[count($array)-1];
		return $item;
    }
	
	
    public function replace_word($filename, $begin="", $end="", $content){
		   if($end=="")
				exec("sudo sed '/".$begin."/c\\".$content."' ".$filename." > ".$this->direc.$this->get_name_file($filename));
		   else
				exec("sudo sed '/".$begin."/,/".$end."/c\\".$content."' ".$filename." > ".$this->direc.$this->get_name_file($filename));
		   exec("sudo  mv ".$filename." ".$this->direc.$this->get_name_file($filename).".bk");
		   exec("sudo  mv ".$this->direc.$this->get_name_file($filename)." ".$this->get_direc($filename).$this->get_name_file($filename));
		   $service= array("dhcpd");
		   $this->state_service($service, "restart");

    }
    
    public function link($source,$dest){
    
		  exec("sudo ln -s ".$source." ".$dest);
    }

   	public function state_service($service_values,$state=""){
	
		  foreach($service_values as $key) {
           exec("sudo /etc/rc.d/".$key." ".$state);
        }
	}

}
?>
